﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class EnemyController : MonoBehaviour
{
    Animator am;
    //SpriteRenderer sr;
    public float Atk_distance;
    float distance;
    bool isAttack;
    [HideInInspector]
    public bool isDmg;
    float dmgTime;
    float attackTime;
    int jumpCount;
    float lastAttackTime;
    bool isAddScore;

    public int npcScore;
    public Transform Player;
    public Text score;
    public int hp;
    public float Speed = 14;
    public float Jump_Power = 40;
    public int Atk;

    void Start()
    {
        am = GetComponent<Animator>();
        //sr = transform.FindChild("EnemySprite").GetComponent<SpriteRenderer>();
    }

    void Update()
    {
        // 判断人物高度是否处于游戏最低点
        if (transform.position.y <= -45)
        {
            Destroy(gameObject);
        }

        // 检测血量
        if (hp <= 0)
        {
            Destroy(gameObject, 0.7F);
            // 游戏加分
            if (!isAddScore)
            {
                isAddScore = true;
                score.text = "Score  " + (int.Parse(score.text.Split(' ')[2]) + npcScore).ToString().PadLeft(6, '0');
            }
        }

        // 被攻击状态转换
        dmgTime += Time.deltaTime;
        if (isDmg)
        {
            if (dmgTime >= 1.2F)
            {
                isDmg = false;
                dmgTime = 0;
            }
        }
    }



    void OnTriggerStay2D(Collider2D other)
    {
        if (Player != null)
        {
            distance = Player.position.x - transform.position.x;
            if (other.name.Equals("Collider_EnemyActiveZone"))
            {
                //sr.sortingLayerName = "Player";// 切换图层
                am.SetTrigger("Run");
                Move();
                Attack();
            }
        }
    }

    void Move()
    {
        // 面朝方向
        if (distance >= 0)
        {
            transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
        }
        else
        {
            transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x) * -1, transform.localScale.y, transform.localScale.z);
        }

        // 移动方向
        if (distance >= Atk_distance)
        {
            if (!isAttack && !isDmg)
            {
                transform.Translate(Vector2.left * -Speed * Time.deltaTime);
                Jump();
            }
        }
        if (distance <= -Atk_distance)
        {
            if (!isAttack && !isDmg)
            {
                transform.Translate(Vector2.left * Speed * Time.deltaTime);
                Jump();
            }
        }
    }
    void Jump()
    {
        if (Physics2D.Raycast(transform.position, Vector2.down, 0.5F))
        {
            jumpCount = 0;
        }
        if (jumpCount < 2)
        {
            if (Player.position.y - transform.position.y > 1)
            {
                jumpCount++;
                gameObject.GetComponent<Rigidbody2D>().velocity = Vector2.up * Jump_Power;
            }
        }
    }
    void Attack()
    {
        if (Time.time - lastAttackTime >= 1.5F)
        {
            if (distance <= Atk_distance && distance >= 0 || distance >= -Atk_distance && distance <= 0)
            {
                am.ResetTrigger("Run");
                am.SetTrigger("Attack_A");
                lastAttackTime = Time.time;
                isAttack = true;
            }
            attackTime += Time.deltaTime;
            if (attackTime >= 2.0F)
            {
                isAttack = false;
                attackTime = 0;
            }
        }
    }

    void OnTriggerExit2D(Collider2D other)
    {
        if (other.name.Equals("Collider_EnemyActiveZone"))
        {
            am.ResetTrigger("Run");
            am.ResetTrigger("Attack_A");
            am.SetTrigger("Idle");
        }
    }

}
